<template>
  <page-dialog
    :title="dialogInfo.title"
    v-model:visible="shows"
    :width="dialogInfo.width"
    :bt-list="dialogInfo.btList"
  >
    <!-- 条件栏 -->
    <page-filter
      v-model:query="filterInfo.query"
      :filter-list="filterInfo.list"
    />
    <page-table
      :refresh="tableInfo.refresh"
      :init-curpage="tableInfo.initCurpage"
      v-model:data="tableInfo.data"
      :api="api"
      :query="filterInfo.query"
      :field-list="tableInfo.fieldList"
      :handle="tableInfo.handle"
      :highlight-row="highlightRow"
      :check-box="checkBox"
      :isDynamic="true"
      @handleEvent="handleEvent"
    >
      <template v-slot:col-licenseNumber="scope">
        <p>{{ scope.row.licenseNumber || '-' }}</p>
        <p>{{ scope.row.brandName || '-' }}</p>
      </template>
      <template v-slot:col-commonName="scope">
        <p>{{ scope.row.commonName || '-' }}</p>
        <p>{{ scope.row.productCode || '-' }}</p>
        <p>{{ scope.row.alias || '-' }}</p>
      </template>
      <template v-slot:col-type="scope">
        <p>{{ scope.row.productTypeValue || '-' }}</p>
        <p>{{ scope.row.codeTypeValue || '-' }}</p>
        <p>{{ scope.row.productCategoryValue || '-' }}</p>
      </template>
      <template v-slot:col-standard="scope">
        <p>{{ scope.row.standard || '-' }}</p>
        <p>{{ scope.row.unitName || '-' }}</p>
      </template>
    </page-table>
  </page-dialog>
</template>
<script setup name="SelectProduct">
import PageDialog from '@/components/PageDialog/index'
import PageTable from '@/components/DialogTable/index'
import PageFilter from '@/components/PageFilter/index'
// import { getPurchProductWithStock } from "@/api/purch.js";

const { proxy } = getCurrentInstance()
const emit = defineEmits()
const props = defineProps({
  highlightRow: {
    type: Boolean,
    default: false,
  },
  checkBox: {
    type: Boolean,
    default: true,
  },
  api: {
    type: Function,
    default: '',
    // default: getPurchProductWithStock,
  },
  showDialog: {
    type: Boolean,
    default: false,
  },
  query: {
    type: Object,
    default: function () {
      return {}
    },
  },
  showStock: {
    type: Boolean,
    default: false,
  },
})
const shows = computed({
  get() {
    return props.showDialog
  },
  set(newValue) {
    emit('update:showDialog', newValue) // 更新父组件shows
  },
})

// 过滤相关配置
const filterInfo = reactive({
  query: {
    searchType: 'product',
    sort: 'common_name,standard',
    status: '1',
    ...props.query,
  },
  list: [
    {
      type: 'input',
      label: '名称/别名/编码/助记符',
      value: 'keyword',
      event: search,
    },
    { type: 'button', label: '搜索', event: search },
  ],
})
// 表格相关
const tableInfo = reactive({
  refresh: 1,
  initCurpage: 1,
  data: [],
  fieldList: [
    {
      label: '通用名称\n编码\n别名',
      value: 'commonName',
      minWidth: 220,
      type: 'slot',
    },
    { label: '规格\n计量单位', value: 'standard', type: 'slot' },
    {
      label: '库存量',
      value: 'defaultDepotStockQty',
      type: 'column',
      formatter: proxy.$fn.qtyFormatter,
    },
    {
      label: '上月出库量',
      value: 'defaultExportQty',
      type: 'column',
      formatter: proxy.$fn.qtyFormatter,
    },
    { label: '供应商', value: 'supplierName' },
    {
      label: '单价',
      value: 'defaultSupplierPrice',
      type: 'column',
      formatter: proxy.$fn.TripartiteMethod,
    },
    {
      label: '产品类型\n条码类型\n产品分类',
      value: 'type',
      minWidth: 150,
      type: 'slot',
    },
    {
      label: '证号\n生产厂家',
      value: 'licenseNumber',
      minWidth: 220,
      type: 'slot',
    },
    { label: '助记符', value: 'mnemonic', width: '100px' },
  ],
})
// 弹窗相关
const dialogInfo = reactive({
  title: '选择产品',
  width: '90%',
  btLoading: false,
  btList: [
    { label: '关闭', event: handleClose, show: true },
    {
      label: '保存',
      type: 'primary',
      disabled: true,
      event: save,
      show: true,
    },
  ],
})
const selectRow = ref('')
onMounted(() => {
  proxy.$nextTick(() => {
    if (props.showStock) {
      tableInfo.fieldList.push({
        label: '库存数量',
        value: 'stockQty',
        type: 'column',
        formatter: proxy.$fn.qtyFormatter,
      })
    }
    getList()
  })
})

// 获取列表
function getList() {
  tableInfo.refresh = Math.random()
}
function search() {
  tableInfo.initCurpage = Math.random()
  tableInfo.refresh = Math.random()
}
function save() {
  // emit('input', data)
  if (selectRow.value) {
    handleClose()
    emit('callback', selectRow.value)
  } else {
    proxy.$message({
      type: 'warning',
      duration: 3500,
      showClose: true,
      message: '请选择产品',
    })
  }
}
function handleEvent(event, data) {
  switch (event) {
    case 'tableClickRow':
      selectRow.value = data
      dialogInfo.btList[1].disabled = !selectRow.value
      break
    case 'tableCheck':
      selectRow.value = data
      dialogInfo.btList[1].disabled = !data.length
      break
  }
}
// 关闭弹窗前的回调
function handleClose() {
  shows.value = false
  emit('reset')
}
</script>
